<template>
  <el-drawer :title="drawerTitle" :visible.sync="drawerShow" size="85%">
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        :option="option"
        :page.sync="page"
        :before-open="beforeOpen"
        :defaults.sync="defaults"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="rowSave"
        @size-change="sizeChange"
        @current-change="currentChange">
      </avue-crud>
    </basic-container>
  </el-drawer>
</template>

<script>
import avue from '@/mixins/avue.js'
import { SE } from '@/common/constants.js'
import {
  dict_detail,
  dict_item_tree,
  dict_item_add,
  dict_item_edit,
  dict_item_delete
} from '@/api/core/system'

export default {
  mixins: [avue],
  computed: {
    drawerTitle () {
      return this.dictInfo.name + '明细'
    }
  },
  watch: {
    'dictInfo.isItemTree' (val) {
      this.$nextTick(() => {
        if (val === 1) {
          this.defaults.parentId.display = true
        } else {
          this.defaults.parentId.display = false
        }
      })
    }
  },
  data () {
    return {
      drawerShow: false,
      dictInfo: {},
      defaults: {},
      data: [],
      option: {
        column: [
          {
            label: '父级节点',
            prop: 'parentId',
            type: 'tree',
            props: {
              label: 'text',
              value: 'id'
            },
            hide: true,
            display: false
          },
          {
            label: '显示值',
            prop: 'text',
            align: 'left'
          },
          {
            label: '实际值',
            prop: 'value',
            align: 'left'
          },
          {
            label: '备注',
            prop: 'remarks',
            type: 'textarea',
            hide: true
          },
          {
            label: '排序',
            prop: 'sort',
            type: 'number',
            value: "1"
          },
          {
            label: '状态',
            prop: 'status',
            type: 'radio',
            dicData: SE,
            value: "1"
          }
        ]
      }
    }
  },
  methods: {
    dictDetail (id) {
      dict_detail(id).then((res) => {
        this.drawerShow = true
        const dict = this.$s.res(res)
        this.dictInfo = dict
        this.rowLoad(dict)
      })
    },
    beforeOpen (done, type) {
      const dictInfo = [{ text: '无', id: '' }]
      this.$refs.crud.updateDic('parentId', dictInfo.concat(this.data))
      done()
    },
    rowLoad (dict) {
      dict_item_tree({ code: dict.code }).then((res) => {
        this.data = this.$s.res(res) || []
        if (dict.isItemTree === 1) {
          this.$refs.crud.updateDic('parentId', this.data)
        }
      })
    },
    rowSave (row, loading, done) {
      row.dictId = this.dictInfo.id
      dict_item_add(row).then((res) => {
        this.$s.suc(res)
        this.rowLoad(this.dictInfo)
        done()
        loading()
      })
    },
    rowUpdate (row, loading, done) {
      dict_item_edit(row).then((res) => {
        this.$s.suc(res)
        this.rowLoad(this.dictInfo)
        done()
        loading()
      })
    },
    rowDel (row, index) {
      this.$confirm('确定数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        dict_item_delete({ ids: row.id }).then((res) => {
          this.$s.suc(res)
          this.rowLoad(this.params)
        })
      })
    }
  },
  mounted () { }
}
</script>

<style lang="less" scoped>
.footer {
  display: flex;
}
</style>
